//package com.strive.demo.web.factory;
//
//import com.alibaba.ttl.threadpool.TtlExecutors;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
//
//import java.util.concurrent.*;
//
///**
// * @description:
// * @author: bingcun.chen
// * @Date: 2022/2/9 16:02
// * @Version 1.0
// */
//
//@Slf4j
//@Configuration
//public class ExecutorThreadPoolFactory {
//    @Autowired
//    @Qualifier("ttlExecutorService")
//    private ExecutorService pool;
//    public ExecutorService getPool() {
//        return pool;
//    }
//    @Value("${thread-pool.core-pool-size:10}")
//    private Integer corePoolSize;
//    @Value("${thread-pool.maximum-pool-size:20}")
//    private Integer maximumPoolSize;
//    @Value("${thread-pool.keep-alive-time:100}")
//    private Long keepAliveTime;
//    @Value("${thread-pool.blocking-queue.capacity:100}")
//    private Integer blockingQueueCapacity;
//    @Value("${thread-pool.thread-name-prefix:thread-pool-}")
//    private String threadNamePrefix;
//
//    @Bean
//    public ThreadPoolExecutor buildThreadPool() {
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,
//                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(blockingQueueCapacity),
//                new CustomizableThreadFactory(threadNamePrefix), new CustomerHandler());
//        log.info("[业务线程池]: {}线程池初始化成功, coreSize: {}, maxSize: {}, 阻塞队列大小: {}", threadNamePrefix, corePoolSize,
//                maximumPoolSize, blockingQueueCapacity);
//        int preStartAllCoreThreads = threadPoolExecutor.prestartAllCoreThreads();
//        log.info("[业务线程池]: {}线程池进行预热, 启动核心线程数: {}", threadNamePrefix, preStartAllCoreThreads);
//        return threadPoolExecutor;
//    }
//
//    @Bean
//    public ExecutorService ttlExecutorService(@Qualifier("buildThreadPool") ThreadPoolExecutor threadPoolExecutor) {
//        // 使用阿里的TtlExecutors进行包装, 使之能够传递ThreadLocal上下文
//        return TtlExecutors.getTtlExecutorService(threadPoolExecutor);
//    }
//
//
//    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
//
//    /**
//     * 检查(调度线程池)，每秒执行一次，查看订单的缓冲队列是否有 订单记录，则重新加入到线程池
//     */
//    final ScheduledFuture scheduledFuture = scheduler.scheduleAtFixedRate(new Runnable() {
//        @Override
//        public void run() {
//            //判断缓冲队列是否存在记录
//            if(!CustomerHandler.msgQueue.isEmpty()){
//                //当线程池的队列容量少于WORK_QUEUE_SIZE，则开始把缓冲队列的订单 加入到 线程池
//                if (buildThreadPool().getQueue().size() < corePoolSize) {
//                    buildThreadPool().execute(CustomerHandler.msgQueue.poll());
//                }
//            }
//        }
//    }, 0, 1, TimeUnit.SECONDS);
//
//
//
//}
